package org.zs.miracle.config.security;

import cn.dev33.satoken.stp.StpInterface;
import jakarta.annotation.Resource;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.zs.miracle.entity.system.ResourcesEntity;
import org.zs.miracle.mapper.ResourcesMapper;
import org.zs.miracle.mapper.RoleMapper;
import org.zs.miracle.mapper.RoleResourcesMapper;
import org.zs.miracle.mapper.UserMapper;
import org.zs.miracle.mapper.UserRoleMapper;
import org.zs.miracle.service.auth.IAuthService;
import org.zs.miracle.service.role.model.vo.RoleVo;

@Component
@AllArgsConstructor
public class StpInterfaceImpl implements StpInterface {
  
  @Resource
  UserRoleMapper userRoleRepository;
  @Resource
  RoleResourcesMapper roleResourcesRepository;
  @Resource
  UserMapper userRepository;
  @Resource
  RoleMapper roleRepository;
  @Resource
  ResourcesMapper resourcesRepository;
  @Resource
  IAuthService authService;
  
  /**
   * 获取用户权限列表
   *
   * @param loginId   账号id
   * @param loginType 账号类型
   * @return 权限编码列表
   */
  @Override
  public List<String> getPermissionList(Object loginId, String loginType) {
    // 用户权限集合
    List<ResourcesEntity> permissionsList =
        this.authService.queryPermissionsList(Long.valueOf(loginId.toString()));
    // 权限编码集合
    return permissionsList.stream().map(ResourcesEntity::getCode).toList();
  }
  
  /**
   * 获取用户角色列表
   *
   * @param loginId   账号id
   * @param loginType 账号类型
   * @return 用户角色编码列表
   */
  @Override
  public List<String> getRoleList(Object loginId, String loginType) {
    // 角色列表
    List<RoleVo> roleList = roleRepository.queryUserRolesByUserId(Long.valueOf(loginId.toString()));
    // 转为角色编码集合
    return roleList.stream().map(RoleVo::getCode).toList();
  }
}